package com.aaa.coupon.service.impl;

import com.aaa.coupon.dao.CouponDao;
import com.aaa.coupon.service.CouponService;
import com.aaa.entity.Coupon;
import com.aaa.entity.CouponHistory;
import com.aaa.vo.CouponHistoryVo;
import com.aaa.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @author 荆豪峰
 * @version 1.0
 * #Date: 2023/3/9 19:53
 */
@Service
public class CouponServiceImpl implements CouponService {
    @Autowired
    private CouponDao couponDao;
    @Override
    public Result listCoupon(Integer memberId, Integer useStatus) {
        Date date = new Date();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String format = simpleDateFormat.format(date);
        List<CouponHistoryVo> coupon= couponDao.listCoupon(memberId,useStatus,format);
        return new Result(2000,"查询成功",coupon);
    }

    @Override
    public Result listCouponout(Integer memberId, Integer useStatus) {
        Date date = new Date();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String format = simpleDateFormat.format(date);
        List<CouponHistoryVo> coupon= couponDao.listCouponout(memberId,useStatus,format);
        return new Result(2000,"查询成功",coupon);
    }

    @Override
    public Result getcoupon() {
        Date date = new Date();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String format = simpleDateFormat.format(date);
        List<Coupon> couponHistories = couponDao.getcoupon(format);
        return new Result(2000,"查询成功",couponHistories);
    }

    @Override
    public Result updateCouponout(Integer memberId, Integer couponId) {
         int limit = couponDao.selectLimit(memberId,couponId);
        int perLimit = couponDao.selectLimits(couponId);
         int i=  couponDao.updateCouponout(couponId);
         if (i == 0) {
             return new Result(5000,"来晚了，优惠券被领完了！");
         }else if (limit>=perLimit){
             return new Result(3000,"你已经领取完了！");
         } else {
            int  l=  couponDao.insertCoupon(memberId,couponId);
             return new Result(2000,"领取成功");
         }

    }
}
